<?php
include("utils.php");
if($_SESSION['siteuserId']=="")
{
	header("location:start.php");
}
if($_POST['mode']=="withdraw_money")
{
	withdraw_money();
}
else
{
	main_content("main();");
}

function withdraw_money()
{
	
$fqtch_payment_id=mysql_query("select paymentId,payment_method,creadit_card_no from payment_method where userId='".$_SESSION['siteuserId']."' ");
$num_payment_id=mysql_num_rows($fqtch_payment_id);
	if($num_payment_id>0)
	{
		$rec_payment_id=mysql_fetch_array($fqtch_payment_id);
		 $insert_tblordermaster="insert into tblordermaster set 
			paymentId='".$rec_payment_id['paymentId']."',
			siteuserId='".$_SESSION['siteuserId']."',
			total_amount='".$_POST['ammount1']."',
			grand_total='".$_POST['ammount1']."',
			payment_method='Credit Card',
			card_type='".$rec_payment_id['payment_method']."',
			card_no='".$rec_payment_id['creadit_card_no']."',
			order_date=NOW(),
			expire_date='".$rec_payment_id['expire_date']."'			
		";
		mysql_query($insert_tblordermaster);
		$id=mysql_insert_id();
		 $insert_query="insert into tblorderdetail set 
			siteuserId='".$_SESSION['siteuserId']."',
			order_id='".$id."',
			adspace_price='".$_POST['ammount1']."',
			payment_reason='Withdraw',
			type='db',
			payout_date=NOW(),
			payment_status='N'
		";
		mysql_query($insert_query)or die(mysql_error());
	}
	else
	{
		$GLOBALS['msg']="Please enter your credit card information into your my account page . ";
	}
main_content("main();");
}
function main()
{

 $sql="select * from tblordermaster OM , tblorderdetail OD where OM.order_id=OD.order_id and OD.siteuserId='".$_SESSION['siteuserId']."' and OD.type='db' and OD.payment_status='Y' ";

$query=mysql_query($sql);

 $sql_all="select * from tblordermaster OM , tblorderdetail OD where OM.order_id=OD.order_id and OD.siteuserId='".$_SESSION['siteuserId']."' ";

$query_all=mysql_query($sql_all);

?>

<script language="javascript" type="text/javascript" src="JavaScripts/datetimepicker.js"></script>

<script language="JavaScript">

function show_div()
{
	//alert("test");
	if(document.withdraw_form.flag.value=="0")
	{
		document.getElementById('submit_form').style.display='';
		document.withdraw_form.flag.value=1;
	}
	else
	{
	document.getElementById('submit_form').style.display='none';
		document.withdraw_form.flag.value=0;
	}
}

function check_value()
{
	
if(document.withdraw_form.ammount1.value=="")
		{
			document.getElementById('err_msg').style.color="red";
			document.getElementById('err_msg').innerHTML="Please enter ammount";
			document.withdraw_form.ammount1.focus();
			return false;
		}

var value_float=parseFloat(document.withdraw_form.ammount1.value);
//alert(value_float);
var total_ammount=parseFloat(document.withdraw_form.total_ammount.value);

if(value_float > total_ammount)
		{
			document.getElementById('err_msg').style.color="red";
			document.getElementById('err_msg').innerHTML="you can withdraw money max $ "+total_ammount;
			document.withdraw_form.ammount1.focus();
			return false;
		}



		return true;
}

function check()
{
	if(document.search_form.startDate.value=="")
		{
			//alert("please enter start date");
			document.getElementById('err_text1').style.color="red";
			document.search_form.startDate.focus();
			return false;
		}
	if(document.search_form.endDate.value=="")
		{
			//alert("please enter start date");
			document.getElementById('err_text2').style.color="red";
			document.search_form.endDate.focus();
			return false;
		}
	return true;
}
</script>
<table width="90%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td valign="top" align="center"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
                  <tr>
                    <td valign="top"></td>
                    <td valign="top"><table width="98%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="11"><img src="images/PinkBorderTopLeft.jpg" alt="" width="11" height="11"></td>
                              <td class="PinkBoxTopBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                              <td width="11"><img src="images/PinkBorderTopRight.jpg" alt="" width="11" height="11"></td>
                            </tr>
                        </table></td>
                      </tr>
                      <tr>
                        <td class="PinkTwosideBorder" valign="top"><strong>Earnings and Payment History</strong><table style="width: 100%;" align="center">
<tbody><tr>
  <td align="center">
    <table style="width: 75%;" align="center">
      <tbody><tr>
        <td align="left"><strong>Adjustments</strong></td>
      </tr>
      <tr>
        <td>
			<table id="adjustmentTable" style="width: 100%; text-align: center;" class="zebra1" cellpadding="1" cellspacing="1" border="1">
			<tr><TD>

          <table id="adjustmentTable" style="width: 100%; text-align: center;"  cellpadding="1" cellspacing="1" class="login_txt">
            <tbody><tr>
              <td class="listHeader"><!--<img id="expandCollapseIcon" src="themes/etology/images/icon-status-collapse.png" onclick="changeIcon(this, 'adjustmentTable');">--> <span onclick="changeIcon(this, 'adjustmentTable');">Show</span></td>
             
              <td style="width: 15%;" class="listHeader">Amount</th>
              <td style="width: 15%;" class="listHeader">Reason</th>
              <td style="width: 20%;" class="listHeader">Description</td>

              <td style="width: 25%;" class="listHeader" nowrap="true">Payment Date</td>

			  <td style="width: 30%;" class="listHeader" nowrap="true">Payment Status</td>
              </tr>
				<?php
				$num_all=mysql_num_rows($query_all);
				if($num_all>0)
				{
					while($rec=mysql_fetch_array($query_all))
					{
				?>
						<tr>
					<td>&nbsp;</td>
					
					<td style="width: 15%;" class="inner_contentArea">

<?php
if($rec['type']=='cr')
{
?>
	<font color="Green">+</font>
<?php
}
else
{
?>
	<font color="Red">-</font>
<?php
}
 echo'$'. $rec['adspace_price'];?>
</th>
					<td style="width: 15%;" class="inner_contentArea"><?php echo $rec['payment_reason'];?></th>
					<td style="width: 35%;" class="inner_contentArea">
					<?php

					 if($rec['type']=='cr')
					{
					?>
					<font color="Green">Credited</font>
					<?php
					}
					else
					{
					?>
					<font color="Red">Debited</font>

<?php
					if($rec['payment_status']=='N')
					{
					?>
					<font color="Red">( Applied )</font>
					<?php
					}
					
					?>

					<?php
					}

					?>

</td>
		
					<td style="width: 20%;" class="inner_contentArea"><?php echo $rec['order_date'];?></td>

					<td style="width: 20%;" class="inner_contentArea">
					<?php
					if($rec['payment_status']=='Y')
					{
					?>
					<font color="Green">Paid</font>
					<?php
					}
					else
					{
					?>
					<font color="Red">Not Paid</font>
					<?php
					}
					?>
					</td>

					</tr>
		
			<?php
					}
				}
			else
			{
			?>
              <tr style="display: table-row;">
              <td>&nbsp;</td>
              <td colspan="4" class="inner_contentArea" style="color:red;">No record.</td>
            </tr>
			<?php
			}
			?>
            </tbody></table>

			</TD>
			</tr>
			</table>

        </td>
      </tr>
      <tr>
        <td style="padding-top: 15px;" align="left">
          <div style="width: 380px;">
          <strong>Payments</strong>
                              </div>
                  </td>
      </tr>

      <tr>
        <td style="padding-left: 35px; padding-top: 0px;" align="left">
                  </td>
      </tr>
      <tr>
        <td align="left">
			<table id="adjustmentTable" style="width: 100%; text-align: center;" class="zebra1" cellpadding="1" cellspacing="1" border="1">
			<tr><TD>

				<table id="paymentHistoryTable" style="width: 100%; -moz-user-select: none;" class="login_txt" cellpadding="0" cellspacing="1">
			<tbody>
			<tr>
            <td class="listHeader" align="center">
        	Payout Date Range
   			</td>
            <td class="listHeader" align="center">
             Payment Date
   			</td>
            <td class="listHeader" align="center">
        	Payout Method
    		</td>
            <td class="listHeader" align="center">
             Status
    		</td>
            <td class="listHeader" align="center">
        	Amount
    		</td>
          </tr>
			<?php
			$num_db=mysql_num_rows($query);
			if($num_db>0)
			{
				while($row=mysql_fetch_array($query))
				{
				?>
			<tr>
				<td align="center" class="inner_contentArea">
				<?php echo $row['payout_date'];?>
				</td>
				<td align="center" class="inner_contentArea">
				<?php echo $row['order_date'];?>
				</td>
				<td align="center" class="inner_contentArea">
				<?php echo $row['payment_method'];?>
				</td>
				<td align="center" class="inner_contentArea">
				<?php echo $row['payment_status'];?>
				</td>
				<td align="center" class="inner_contentArea">
				<?php echo $row['adspace_price'];?>
				</td>
			</tr>
			<?php
				}
			}
			else
			{
				?>
        
  <tr id="noRecordRow"><td colspan="5" style="text-align: center;color:red;" class="tablebackgroundcolor">No record.</td></tr>
			<?php
			}
			?>
  	</tbody>
	</table>

		</td>
		</tr>
		</table>
       


        </td>
      </tr>
      <tr style="text-align: center;">
        <td>
          <form id="searchForm" name="search_form" method="post" action="" style="padding-top: 20px; padding-bottom: 0px;" class="inner_contentArea" onsubmit="return check();">
			<input type="hidden" name="sub" value="sub">
            <span id="err_text1">Show stats from</span>:
            <input name="startDate" type="text" class="InputBox" id="startDate" style="width: 80px;" maxlength="24" value="" onclick="javascript:NewCal('startDate','yyyymmdd',false,24);"> <span id="err_text2">Start Date
            to</span>:
            <input name="endDate" type="text" class="InputBox" id="endDate" style="width: 80px;" maxlength="24" value="" onclick="javascript:NewCal('endDate','yyyymmdd',false,24);">

<a class="dateformat">End Date</a>
            <input id="runId" name="search" value="Run" class="button-go" type="submit">
          </form>
     
        </td>

      </tr>
    </tbody></table>
  </td>
  </tr>
  <tr>
    <td style="padding-left: 35px;" class="inner_contentArea"><h3 style="text-align: left;">Earnings<br> <br>You can view your Commercial break earnings stats by using your mouse to hover over the "Sales" column under "Network Ads".  <br><br>*NOTE: Commercial break ad impressions are counted as clicks under Network Ads.</h3></td>
  </tr>

  <tr>
    <td>
      <table id="adjustmentTable" style="width: 100%; text-align: center;" class="zebra1" cellpadding="1" cellspacing="1" border="1">
			<tr><TD>
			
<?php

if($_POST[sub]=="sub")
{
 $sql_advertise="select * from advertise_master AM ,advertise_details AD,tblorderdetail OD where AM.advertiseId=AD.advertiseId and AD.advertiseId=OD.advertiseId and AM.siteuserId='".$_SESSION['siteuserId']."'";

$sql_advertise.=" and AM.startDate > '".$_POST['startDate']."' and endDate < '".$_POST['endDate']."' ";

$sql_advertise.=" group by AD.advertiseId ";
}
else
{
	$sql_advertise="select * from advertise_master AM ,advertise_details AD,tblorderdetail OD where AM.advertiseId=AD.advertiseId and AD.advertiseId=OD.advertiseId and AM.siteuserId='".$_SESSION['siteuserId']."' group by AD.advertiseId ";
}

//echo $sql_advertise;

$query_advertise=mysql_query($sql_advertise);
 $num_advertise=mysql_num_rows($query_advertise);
?>
			<table id="earningTable" style="width: 100%; text-align: center;" algin="center" class="login_txt" cellpadding="1" cellspacing="1">
          <tbody>
			<tr>
            <td class="listHeader">Show</th>
            <td class="listHeader">Date</td>
            <td class="listHeader">Direct Ad</td>
            <td class="listHeader">Network Ads</th>
            <td class="listHeader">Sales Total</td>
            <td class="listHeader">Payout Rate</td>
            <td class="listHeader">Sales</td>
            <td class="listHeader">Total</td>
          </tr>
		<?php
		if($num_advertise>0)
			{
				while($row_advertise=mysql_fetch_array($query_advertise))
				{
					$count_adspaceId=mysql_fetch_array(mysql_query("select count(*) from advertise_details where adspaceId='".$row_advertise['adspaceId']."' "));

					
		?>
			<tr>
            <td>&nbsp;</th>
            <td><?php echo $row_advertise['startDate']." to ".$row_advertise['endDate'];?></td>
            <td><?php if($row_advertise['networkId']=="0") echo "Yes"; else echo "No";?></td>
            <td><?php if($row_advertise['networkId']!="0") echo "Yes"; else echo "No";?></th>
            <td><?php echo $count_adspaceId['0'];?></td>
            <td><?php echo "$". $row_advertise['adPrice'];?></td>
            <td>Sales</td>
            <td><?php echo $price=($row_advertise['adPrice']*$count_adspaceId['0']);?></td>
          </tr>
		<?php
				}
			}
		else
			{
		?>
          <tr>
            <td colspan="9" style="color:red;">No record.</td>
          </tr>
			<?php
				}
			?>
          </tbody>
		  </table>
			
			</td></tr></table>
    </td>
  </tr>

  <tr id="footnote" name="footnote" style="visibility: hidden;">
    <td style="text-align: right;">
      Dates based on GMT-08:00.
    </td>
  </tr>
  <tr align="center">
    <td align="center">
   			<table id="adjustmentTable" style="width: 30%; text-align: center;" class="zebra1" cellpadding="1" cellspacing="1" border="1">
			<tr><TD>

			<table id="subtotalTable" style="width: 30%;" algin="center" class="login_txt" cellpadding="1" cellspacing="1">

      <tbody><tr align="center">
        <td colspan="2" nowrap="true" class="listHeader">Earnings and Payments Summary</td>
      </tr>
      <tr>
        <td style="text-align: left; font-weight: bold; padding-left: 1em;">Total Earned:</td>
        <td id="totalEarned" name="total_earned" style="text-align: center; width: 40%;padding-right:10px;padding-left:10px;">
<?php
 $sql_total_earned=mysql_fetch_array(mysql_query("select sum(adspace_price) from tblorderdetail where siteuserId='".$_SESSION['siteuserId']."' and type='cr' and payment_status='Y' "));
echo "$".$sql_total_earned['0'];
?>
</td>
      </tr>

      <tr>
        <td style="background-color: rgb(232, 232, 232); text-align: left; font-weight: bold; padding-left: 1em;">Total Paid:</td>
        <td id="totalPaid" name="total_paid" style="background-color: rgb(232, 232, 232); text-align: center;">
<?php
 $sql_total_paid=mysql_fetch_array(mysql_query("select sum(adspace_price) from tblorderdetail where siteuserId='".$_SESSION['siteuserId']."' and type='db' and payment_status='Y' "));
echo "$".$sql_total_paid['0'];
?>
</td>
      </tr>
      <tr>
        <td style="text-align: left; font-weight: bold; padding-left: 1em;">Current Balance:</td>
        <td id="totalBalance" name="total_balance" style="text-align: center;">
<?php
echo "$".$balance=($sql_total_earned['0']-$sql_total_paid['0']);

$sql_total_avalable=mysql_fetch_array(mysql_query("select sum(adspace_price) from tblorderdetail where siteuserId='".$_SESSION['siteuserId']."' and type='db' "));

$balance1=($sql_total_earned['0']-$sql_total_avalable['0']);

?>
</td>
      </tr>

		<tr>
        <td style="text-align: left; font-weight: bold; padding-left: 1em;" colspan="2">
<form name="withdraw_form" method="POST" action="" onsubmit="return check_value();">
<input type="hidden" name="flag" value="0">
<input type="hidden" name="mode" value="withdraw_money">
<input type="hidden" name="total_ammount" value="<?php echo $balance1;?>">
	<table width="100%">
	<TR><TD>
	<a href="javascript:show_div();">Withdraw money</a>
	</td></tr>
	<tr><TD align="center">
				<table width="100%" id="submit_form" style="display:none;" align="center">
			
				<TR><TD class="inner_contentArea"><span id="err_msg">&nbsp;</span></TD></TR>
	
				<TR><TD class="inner_contentArea" align="left"><input type="text" name="ammount1" value="" id="ammount1" class="InputBox" style="width: 80px;" maxlength="24"></TD></TR>
				<TR><TD align="left"><input type="submit" name="ammount" value="Submit"></TD></TR>
				</table>
	</TD></tr>
	</table>
</form>
</td>
        </tr>
		

    </tbody></table>

			</td>
			</tr>
			</table>
    </td>
  </tr>
</tbody></table></td>
                      </tr>
                      <tr>
                        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="11"><img src="images/PinkBorderBottomLeft.jpg" alt="" width="11" height="11"></td>
                              <td class="PinkBoxBottomBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                              <td width="11"><img src="images/PinkBorderBottomRight.jpg" alt="" width="11" height="11"></td>
                            </tr>
                        </table></td>
                      </tr>
                    </table></td>
                    <td>&nbsp;</td>
                    </tr>
                </table></td>
              </tr>
            </table>
</form>
            
<?php
        //include("footer.php");
}
		?> 